package fr.pud.client.controller.fleet;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;

import fr.pud.client.i18n.LanguagesConstants;
import fr.pud.client.images.ImagesDatabase;
import fr.pud.client.model.Fleet;
import fr.pud.client.model.PUDStation;
import fr.pud.client.model.Vehicle;
import fr.pud.client.view.AddVehicleFrame;

/**
 * 3 avr. 2012 - Projet_Ose
 * @author Simon Devineau, Adrien Hierry, Leopold Houdin, Pierre Reliquet
 *         Ecole des Mines de Nantes
 *         ValidateForAddingFleet.java
 */
public class CheckAndAddVehicleToFleet extends AbstractAction {
    /**
     * The serial UID
     */
    private static final long serialVersionUID = 8043907715176747301L;

    /**
     * Default constructor
     */
    public CheckAndAddVehicleToFleet() {
        this.putValue(NAME, LanguagesConstants.LANGUAGE
                .getString(LanguagesConstants.ADD_VEHICLE));
        this.putValue(SHORT_DESCRIPTION, LanguagesConstants.LANGUAGE
                .getString(LanguagesConstants.VALIDATE_DESCRIPTION));
        this.putValue(SMALL_ICON, ImagesDatabase.PLUS);
    }

    /**
     * Tests if the inputs are correct and add the vehicle to the fleet if they
     * are correct and do nothing else.
     */
    @Override
    public void actionPerformed(ActionEvent aArg0) {
        String capacity = AddVehicleFrame.getInstance().getCapacity().getText();
        String maxDriveduration = AddVehicleFrame.getInstance()
                .getMaxDriveDuration().getText();
        PUDStation startingPoint = (PUDStation) AddVehicleFrame.getInstance()
                .getStartingPoint().getSelectedItem();
        PUDStation endingPoint = (PUDStation) AddVehicleFrame.getInstance()
                .getEndingPoint().getSelectedItem();
        if (OpenFrameAddVehicle.checkInputUser(capacity, maxDriveduration,
                startingPoint, endingPoint)) {
            Fleet.getInstance().add(
                    new Vehicle(Integer.parseInt(capacity), Integer
                            .parseInt(maxDriveduration), startingPoint,
                            endingPoint));
            AddVehicleFrame.getInstance().getCapacity().setText("");
            AddVehicleFrame.getInstance().getMaxDriveDuration().setText("");
            if (Fleet.getInstance().getVehicles().size() == 0) {
                AddVehicleFrame.getInstance().getModify().setVisible(false);
                AddVehicleFrame.getInstance().getDelete().setVisible(false);
            }
            else {
                AddVehicleFrame.getInstance().getModify().setVisible(true);
                AddVehicleFrame.getInstance().getDelete().setVisible(true);
            }
        }
    }
}
